package com.wsj.message_server.utils;

import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.*;
import com.tencent.xinge.bean.ios.Aps;
import com.tencent.xinge.push.app.PushAppRequest;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PushAppUtil {

    @Value("${androidPush.appId}")
    private String androidAppId;

    @Value("${androidPush.secretKey}")
    private String androidSecretKey;

    @Value("${iosPush.appId}")
    private String iosAppId;

    @Value("${iosPush.secretKey}")
    private String iosSecretKey;

    public String androidApp(String title, String content, String token, String type) {
        XingeApp xingeApp = new XingeApp.Builder()
                .appId(androidAppId)
                .secretKey(androidSecretKey)
                .domainUrl("https://api.tpns.sh.tencent.com/")
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.token_list);
        pushAppRequest.setPlatform(Platform.android);
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        pushAppRequest.setMessage(message);
        MessageAndroid messageAndroid = new MessageAndroid();
        ClickAction clickAction = new ClickAction();
        clickAction.setAction_type(1);
        clickAction.setActivity("com.xcs.apsara.svideo.activity.MainActivity");
        messageAndroid.setAction(clickAction);
        messageAndroid.setRing(1);
        JSONObject postJson = new JSONObject();
        Map<String, Object> map = new HashMap<>();
        map.put("page", type);
        postJson.put("ext", map);
        messageAndroid.setCustom_content(postJson.toString());
        message.setAndroid(messageAndroid);
        ArrayList<String> tokenList = new ArrayList<>();
        tokenList.add(token);
        pushAppRequest.setToken_list(tokenList);
        // pushAppRequest.setUploadId(8376);
        //xingeApp.pushApp(pushAppRequest);
        JSONObject ret = xingeApp.pushApp(pushAppRequest);
        System.out.println(messageAndroid.getCustom_content());
        return "11";

    }

    public String iosApp(String title, String content, String token, String type) {
        XingeApp xingeApp = new XingeApp.Builder()
                .appId(iosAppId)
                .secretKey(iosSecretKey)
                .domainUrl("https://api.tpns.sh.tencent.com/")
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.token_list);
        pushAppRequest.setPlatform(Platform.ios);
        pushAppRequest.setMessage_type(MessageType.notify);
        pushAppRequest.setEnvironment(Environment.dev);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        pushAppRequest.setMessage(message);
        MessageIOS messageIOS = new MessageIOS();
//    ClickAction clickAction=new ClickAction();
//    clickAction.setAction_type(1);
//    clickAction.setActivity("com.xcs.apsara.svideo.activity.MainActivity");
        JSONObject postJson = new JSONObject();
        Map<String, Object> map = new HashMap<>();
        map.put("page", type);
        postJson.put("ext", map);
        messageIOS.setCustom(postJson.toString());
        Aps aps=new Aps();
        aps.setSound("default");
        messageIOS.setAps(aps);
        message.setIos(messageIOS);
        ArrayList<String> tokenList = new ArrayList<>();
        tokenList.add(token);
        pushAppRequest.setToken_list(tokenList);
        // pushAppRequest.setUploadId(8376);
        //xingeApp.pushApp(pushAppRequest);
        JSONObject ret = xingeApp.pushApp(pushAppRequest);
        System.out.println(messageIOS.getCustom());
        return "11";

    }

    public static void main(String[] args) {
         PushAppUtil pushAppUtil = new PushAppUtil();
//        List<String> token=new ArrayList<>();
//        token.add("088ba6f8d2eed6b39939574711a6ce0b6115");
        pushAppUtil.iosApp("小宠书","新作品","088ba6f8d2eed6b39939574711a6ce0b6115","material");
//        pushAppUtil.app(null, null, null);
//        String filePath = "D:\\token.txt";
//        File file = new File(filePath);
//        RequestBody requestBody = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM)
//                .addFormDataPart("file", file.getName(),
//                        RequestBody.create(MediaType.parse("multipart/form-data"), file))
//                .build();
//        String url = "https://api.tpns.sh.tencent.com/v3/push/package/upload";
//        String authString = "1580003761" + ":" + "508ceb49e4eae15e8bd140a9c870d7d2";
//        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
//        String authStringEnc = new String(authEncBytes);
//        Request.Builder builder = new Request.Builder()
//                .url(url)
//                .post(requestBody)
//                .addHeader("Authorization", "Basic " + authStringEnc);
//        Request request = builder.build();
//        System.out.println(builder);
//        System.out.println(request);
//        JSONObject jsonRet = null;
//        Response response = null;
//        OkHttpClient client = new OkHttpClient();
//        try {
//            response = client.newCall(request).execute();
//            if (response.code() == 200) {
//                String retMsg = response.body().string();
//                jsonRet = new JSONObject(retMsg);
//            } else {
//                jsonRet = new JSONObject();
//                jsonRet.put("retCode", 10101);
//                jsonRet.put("errMsg", "CallApiError,HttpStatus Code:" + response.code());
//            }
//        } catch (IOException e) {
//            jsonRet = new JSONObject();
//            jsonRet.put("retCode", 10100);
//            jsonRet.put("errMsg", e.getMessage());
//        } finally {
//            if (response != null) {
//                response.close();
//            }
//        }
//        System.out.println(jsonRet.toString());
////    }

    }

    public String androidAppList(String title, String content, List<String> token, String type,long materialId,int typeId,long poster) {
        XingeApp xingeApp = new XingeApp.Builder()
                .appId(androidAppId)
                .secretKey(androidSecretKey)
                .domainUrl("https://api.tpns.sh.tencent.com/")
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.token_list);
        pushAppRequest.setPlatform(Platform.android);
        pushAppRequest.setMessage_type(MessageType.notify);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        pushAppRequest.setMessage(message);
        MessageAndroid messageAndroid = new MessageAndroid();
        ClickAction clickAction = new ClickAction();
        clickAction.setAction_type(1);
        clickAction.setActivity("com.xcs.apsara.svideo.activity.MainActivity");
        messageAndroid.setAction(clickAction);
        messageAndroid.setRing(1);
        JSONObject postJson = new JSONObject();
        Map<String, Object> map = new HashMap<>();
        map.put("page", type);
        map.put("materialId",materialId);
        map.put("typeId",typeId);
        map.put("poster",poster);
        postJson.put("ext", map);
        messageAndroid.setCustom_content(postJson.toString());
        message.setAndroid(messageAndroid);
        ArrayList arrayList = new ArrayList(token);
        pushAppRequest.setToken_list(arrayList);
        // pushAppRequest.setUploadId(8376);
        //xingeApp.pushApp(pushAppRequest);
        JSONObject ret = xingeApp.pushApp(pushAppRequest);
        System.out.println(messageAndroid.getCustom_content());
        return "11";
    }

    public String iosAppList(String title, String content, List<String> token, String type,long materialId,int typeId,long poster) {
        XingeApp xingeApp = new XingeApp.Builder()
                .appId(iosAppId)
                .secretKey(iosSecretKey)
                .domainUrl("https://api.tpns.sh.tencent.com/")
                .build();
        PushAppRequest pushAppRequest = new PushAppRequest();
        pushAppRequest.setAudience_type(AudienceType.token_list);
        pushAppRequest.setPlatform(Platform.ios);
        pushAppRequest.setMessage_type(MessageType.notify);
        pushAppRequest.setEnvironment(Environment.dev);
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        pushAppRequest.setMessage(message);
        MessageIOS messageIOS = new MessageIOS();
//    ClickAction clickAction=new ClickAction();
//    clickAction.setAction_type(1);
//    clickAction.setActivity("com.xcs.apsara.svideo.activity.MainActivity");
        JSONObject postJson = new JSONObject();
        Map<String, Object> map = new HashMap<>();
        map.put("page", type);
        map.put("materialId",materialId);
        map.put("typeId",typeId);
        map.put("poster",poster);
        postJson.put("ext", map);
        Aps aps=new Aps();
        aps.setSound("default");
        messageIOS.setAps(aps);
        messageIOS.setCustom(postJson.toString());
        message.setIos(messageIOS);
        ArrayList arrayList = new ArrayList(token);
        pushAppRequest.setToken_list(arrayList);
        // pushAppRequest.setUploadId(8376);
        //xingeApp.pushApp(pushAppRequest);
        JSONObject ret = xingeApp.pushApp(pushAppRequest);
        System.out.println(messageIOS.getCustom());
        return "11";
    }
}
